﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using log4net;
using WEB.BLL.Entities;
using WEB.Exceptions;

namespace WEB.BLL.Services
{
    #region ScreenService

    /// <summary>
    /// ScreenService object for Ibatis mapped table 'Screen'.
    /// </summary>
    public class ScreenService : IScreenService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ScreenService).Name);
        private static volatile ScreenService _ScreenService = null;
        public static ScreenService GetInstance()
        {
            if (_ScreenService == null)
            {
                lock (typeof(ScreenService))
                {
                    if (_ScreenService == null) // double-check
                        _ScreenService = new ScreenService();
                }
            }
            return _ScreenService;
        }
        public ScreenService()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public List<ScreenEntity> LoadScreenByModuleId(string moduleId)
        {
            try
            {
                List<ScreenEntity> lstScreen = new List<ScreenEntity>();
                IList<ScreenEntity> screens = this.LoadAll();
                foreach (ScreenEntity screen in screens)
                {
                    if (screen.Module.Equals(moduleId))
                    {
                        lstScreen.Add(screen);
                    }
                }
                return lstScreen;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }

        public IList<ScreenEntity> LoadScreenByModuleAndRole(ScreenEntity objE)
        {
            try
            {
                return ExecuteQueryForList<ScreenEntity>("ScreenLoadByModuleAndRole", objE);
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
        public IList<ScreenEntity> LoadScreenByDesktop(ScreenEntity objE)
        {
            try
            {
                return ExecuteQueryForList<ScreenEntity>("ScreenLoadByDesktop", objE);
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
        public int UpdateName(ScreenEntity objE)
        {
            try
            {
                ExecuteUpdate("ScreenUpdateName", objE);
                return 1;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
        public IList<ScreenEntity> LoadAllEx()
        {
            try
            {
                return ExecuteQueryForList<ScreenEntity>("ScreenLoadAllEx", null);
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
        public IList<ScreenEntity> DashboardLoadAll(ScreenEntity objE)
        {
            try
            {
                return ExecuteQueryForList<ScreenEntity>("ScreenDashboardLoadAll", objE);
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
        public IList<ScreenEntity> InDashboard(ScreenEntity objE)
        {
            try
            {
                return ExecuteQueryForList<ScreenEntity>("ScreenInDashboard", objE);
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
                throw new DBException(ex.Message, ex);
            }
        }
    }
    #endregion
}
